﻿namespace LeetCodeProject._0001_1000._001_100._11_20._019;

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}

public class Solution
{
    public ListNode RemoveNthFromEnd(ListNode head, int n)
    {
        var dummy = new ListNode(0) { next = head };
        var p = dummy;
        var q = p.next;
        for (int i = 0; i < n; i++)
        {
            q = q.next;
        }

        while (q != null)
        {
            p = p.next;
            q = q.next;
        }

        p.next = p.next.next;
        return dummy.next;
    }
}